//
//  Helper.swift
//  worldexchange
//
//  Created by xionghao on 2019/1/9.
//  Copyright © 2019 xionghao. All rights reserved.
//

import UIKit
import RxSwift

extension String {
    
    func coverToOtherCurrent(from:RateItem,to:RateItem)->String{
        let count =  Double(self)!
       
        let selling = Double(to.selling)!
        let buying = Double(to.selling)!
        let value = (selling / buying) * count
        
       return String(format: "%.4f", value)
    }
}

class Utils {
    static func pushLogin() {
        let signVc = SignInViewController()
        let signNav = AppNavViewController(rootViewController: signVc)
        UIApplication.shared.keyWindow?.rootViewController?.present(signNav, animated: true, completion: {
            
        })
    }
    
    public static func timer(duration: RxTimeInterval = RxTimeInterval.infinity, interval: RxTimeInterval = 1, ascending: Bool = false, scheduler: SchedulerType = MainScheduler.instance)
        -> Observable<TimeInterval> {
            let count = (duration == RxTimeInterval.infinity) ? .max : Int(duration / interval) + 1
            return Observable<Int>.timer(0, period: interval, scheduler: scheduler)
                .map { TimeInterval($0) * interval }
                .map { ascending ? $0 : (duration - $0) }
                .take(count)
    }
    

    
    static func pushRegister(){
        let signVc = SignInViewController()
        let signNav = AppNavViewController(rootViewController: signVc)
        let signUpVc = SignUpViewController()
        signUpVc.hidesBottomBarWhenPushed = true
        signNav.pushViewController(signUpVc, animated: false)
        UIApplication.shared.keyWindow?.rootViewController?.present(signNav, animated: true, completion: {
            
        })
    }
    
    static func logout() {
        isLogin = false
        userData = nil
        NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "UserLoginChange"), object: nil)
    }
}
enum Validate {
    case email(_: String)
    case phoneNum(_: String)
    case carNum(_: String)
    case username(_: String)
    case password(_: String)
    case nickname(_: String)
    
    case URL(_: String)
    case IP(_: String)
    
    
    var isRight: Bool {
        var predicateStr:String!
        var currObject:String!
        switch self {
        case let .email(str):
            predicateStr = "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$"
            currObject = str
        case let .phoneNum(str):
            predicateStr = "^((13[0-9])|(15[^4,\\D]) |(17[0,0-9])|(18[0,0-9]))\\d{8}$"
            currObject = str
        case let .carNum(str):
            predicateStr = "^[A-Za-z]{1}[A-Za-z_0-9]{5}$"
            currObject = str
        case let .username(str):
            predicateStr = "^[A-Za-z0-9]{6,20}+$"
            currObject = str
        case let .password(str):
            predicateStr = "^[a-zA-Z0-9]{6,20}+$"
            currObject = str
        case let .nickname(str):
            predicateStr = "^[\\u4e00-\\u9fa5]{4,8}$"
            currObject = str
        case let .URL(str):
            predicateStr = "^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$"
            currObject = str
        case let .IP(str):
            predicateStr = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
            currObject = str
        }
        
        let predicate =  NSPredicate(format: "SELF MATCHES %@" ,predicateStr)
        return predicate.evaluate(with: currObject)
    }
}
